/**
 * Copyright (c) 2016 coderyuan.com. All Rights Reserved.
 *
 * EShop
 *
 * PageUtil.java created on 下午6:13
 *
 * @author yuanguozheng
 * @version 1.0.0
 * @since 16/3/2
 */
package com.coderyuan.web.api.kit.utils;

import com.coderyuan.web.api.kit.models.PageModel;

/**
 * 分页信息辅助类
 *
 * @author yuanguozheng
 */
public class PageUtil {

    public static PageModel createPage(int everyPage, int currentPage, boolean hasNextPage) {
        everyPage = getEveryPage(everyPage);
        currentPage = getCurrentPage(currentPage);
        int beginIndex = getBeginIndex(everyPage, currentPage);
        return new PageModel(everyPage, currentPage, hasNextPage, beginIndex);
    }

    /**
     * 设置每页显示记录数
     *
     * @param everyPage int
     * @return int
     */
    public static int getEveryPage(int everyPage) {
        return everyPage == 0 ? 10 : everyPage;
    }

    /**
     * 设置当前页
     *
     * @param currentPage int
     * @return int
     */
    public static int getCurrentPage(int currentPage) {
        return currentPage == 0 ? 1 : currentPage;
    }

    /**
     * 设置起始点，需要每页显示多少，当前页
     *
     * @param everyPage   int
     * @param currentPage int
     * @return int
     */
    public static int getBeginIndex(int everyPage, int currentPage) {
        return (currentPage - 1) * everyPage;
    }
}
